package com.hunters.web.wechat.controller;

import com.hunters.web.wechat.entity.model.LoginSession;
import com.hunters.web.wechat.service.service.WebWechatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @program: web-wechat
 * @description:
 * @author: 李二
 * @create: 2018/09/05 17:54
 **/
@Slf4j
@Controller
@RequestMapping("wechat")
public class WebWechatController {

    @Autowired
    private WebWechatService webWechatService;

    @RequestMapping("qrcode")
    public Object getQcrode(Model model, HttpServletRequest request, HttpServletResponse response) {
        String uuid = webWechatService.getUUID();
        if (StringUtils.isBlank(uuid)) {
            model.addAttribute("qrcodeUrl", "/static/404.gif");
        } else {
            model.addAttribute("qrcodeUrl", webWechatService.getQRcodeUrl(uuid));
            model.addAttribute("uuid", uuid);
        }
        //context.publishEvent(uuid);
        return "index";
    }

    @RequestMapping("listen")
    @ResponseBody
    public Map<String, Object> loginListen(String uuid) {
        return webWechatService.loginListen(uuid);
    }

    @RequestMapping("syncCheckListen")
    @ResponseBody
    public Map<String, Object> syncCheckListen(@RequestBody LoginSession loginSession) {
        return webWechatService.syncCheck(loginSession);
    }
}
